package com.flappySeeker.common;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BaseDaoImp implements BaseDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	/**
	 * 将结果转化为List<Map<String, Object>> 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public  List<Map<String, Object>> sqllistmap(String sql) throws SQLException{
		return sqllistmap(sql,null);
	}
	
	/**
	 * 将结果转化为List<Map<String, Object>> 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public  List<Map<String, Object>> sqllistmap(String sql,Object... args) throws SQLException{
		return jdbcTemplate.queryForList(sql,args);
	}
	
	/**
	 * 将结果转化为Map<String, Object> 只能返回一行数据，超过一行报错
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public  Map<String, Object> sqlformap(String sql) throws SQLException{
		try{  
			return jdbcTemplate.queryForMap(sql);
        }catch (EmptyResultDataAccessException e) {  
            return null;  
        }  
	}
	
	/**
	 * 执行更新
	 */
	public  int update(String sql) throws SQLException{
		return update(sql,null);
	}
	
	/**
	 * 执行更新
	 */
	public  int update(String sql,Object... args) throws SQLException{
		return jdbcTemplate.update(sql, args);
	}
	
	/**
	 * 返回实体
	 */
	public  Class<?> sqlModle(String sql,Class<?> c) throws SQLException{
		return (Class<?>) jdbcTemplate.queryForObject(sql,null,c);
	}
	
	/**
	 * 查询条数
	 * 如果结果为0条或者多条返回0
	 * @param <T>
	 */
	public  int  sqlforint(String sql,Object... args) throws SQLException{
		try {
            return jdbcTemplate.queryForInt(sql,args);
        } catch (IncorrectResultSizeDataAccessException e) {
                    return 0;
        }
	}
	/**
	 * 查询条数
	 * 如果结果为0条或者多条返回0
	 * @param <T>
	 */
	public  String  sqlforobj(String sql,Object... args) throws SQLException{
		try {
            return jdbcTemplate.queryForObject(sql, String.class,args);
        } catch (IncorrectResultSizeDataAccessException e) {
                    return "0";
        }
	}
	/**
	 * 查询条数
	 * 如果结果为0条或者多条返回0
	 * @param <T>
	 */
	public  String  sqlforobj(String sql) throws SQLException{
            try {
                return jdbcTemplate.queryForObject(sql, String.class);
            } catch (IncorrectResultSizeDataAccessException e) {
            	return "0";
            }
	}
	/**
	 * 获取包含结果的分页对象
	 * @param p Page对象，含查询参数
	 */
	@SuppressWarnings("deprecation")
	public Page getPageResult(Page page){
		String countsql = page.getSql();
		System.out.println(countsql);
		countsql = countsql.replaceFirst("(?<=SELECT).*?(?=FROM)", " COUNT(1) ");
		System.out.println(countsql);
		int listCount = jdbcTemplate.queryForInt(countsql);
		page.setTotalProperty(listCount);
		
		if(page.getOrderBy()!= null){
			page.setSql(page.getSql()+ " order by "+page.getOrderBy()+" "+page.getOrder());
		}
		page.setSql(page.getSql()+" limit "+page.getCurPageStartIndex()+","+page.getLimit());
		page.setResult(jdbcTemplate.queryForList(page.getSql()));
		return page;
	}
	/**
	 * 获取包含结果的分页对象，不拆分sql，效率低
	 * @param p Page对象，含查询参数
	 */
	public Page getByPage(Page page){
		List listCount = jdbcTemplate.queryForList(page.getSql());
		page.setTotalProperty(listCount.size());
		
		if(page.getOrderBy()!= null){
			page.setSql(page.getSql()+ " order by "+page.getOrderBy()+" "+page.getOrder());
		}
		page.setSql(page.getSql()+" limit "+page.getCurPageStartIndex()+","+page.getLimit());
		page.setResult(jdbcTemplate.queryForList(page.getSql()));
		return page;
	}
	
}
